local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    Asset( "IMAGE", "images/map_icons/npc_item_treasure_map.tex" ),  -- 背包贴图
    Asset( "ATLAS", "images/map_icons/npc_item_treasure_map.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_woodlegs_tent"] or {}
end


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- local function container_Widget_change(theContainer)
--     local params = {}
--     params._theContainerWidget =
--     {
--         widget =
--         {
--             slotpos = {},
--             animbank = "ui_fish_box_5x4",
--             animbuild = "ui_fish_box_5x4",
--             pos = Vector3(0, 220, 0),
--             side_align_tip = 160,
--         },
--         type = "chest",
--         acceptsstacks = true,
--         -- usespecificslotsforitems = true,
--         -- type = "none",
--     }

--     for y = 2.5, -0.5, -1 do
--         for x = -1, 3 do
--             table.insert(params._theContainerWidget.widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
--         end
--     end

--     -- function params._theContainerWidget.itemtestfn(container, item, slot)
--     --     return true
--     -- end
--     theContainer:WidgetSetup("wobysmall",params._theContainerWidget)
--     -- if TheWorld.ismastersim then
--     --     theContainer:SetNumSlots(#params._theContainerWidget.widget.slotpos)
--     -- end
-- end
local function container_Widget_change(theContainer)
    -----------------------------------------------------------------------------------
    ----- 容器界面名 --- 要独特一点，避免冲突
        local container_widget_name = "npc_item_woodlegs_treasure_box_widget"

    -----------------------------------------------------------------------------------
    ----- 检查和注册新的容器界面
        local all_container_widgets = require("containers")
        local params = all_container_widgets.params
        if params[container_widget_name] == nil then
            params[container_widget_name] = {
                widget =
                {
                    slotpos = {},
                    animbank = "ui_fish_box_5x4",
                    animbuild = "ui_fish_box_5x4",
                    pos = Vector3(0, 220, 0),
                    side_align_tip = 160,
                },
                type = "chest",
                acceptsstacks = true,                
            }

            for y = 2.5, -0.5, -1 do
                for x = -1, 3 do
                    table.insert(params[container_widget_name].widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
                end
            end
            ------------------------------------------------------------------------------------------
        end

        theContainer:WidgetSetup(container_widget_name)

end
local function add_container_before_not_ismastersim_retrun(inst)
	-------------------------------------------------------------------------------------------------
    ------ 添加背包container组件    --- 必须在 SetPristine 之后，
    -- local container_WidgetSetup = "wobysmall"
    if TheWorld.ismastersim then
        inst:AddComponent("container")
        inst.components.container.openlimit = 1  ---- 限制1个人打开
    --    inst.components.container:WidgetSetup(container_WidgetSetup)
        container_Widget_change(inst.components.container)
            -- inst.components.container.numslots = 9 -- 格子数  (不能用这个，会造成bug)   
    else
        inst:DoTaskInTime(0.1, function()
            if inst.replica then
                if inst.replica.container then
                --    inst.replica.container:WidgetSetup(container_WidgetSetup)
                    container_Widget_change(inst.replica.container)
                end
            end

        end)
    end
    -------------------------------------------------------------------------------------------------
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()
	inst.entity:AddMiniMapEntity()
    inst.entity:AddAnimState()

    TheSim:LoadPrefabs({ "quagmire_crate" })    ----- 暴食里的物品都得用这个加载素材

    inst.AnimState:SetBank("quagmire_crate")
    inst.AnimState:SetBuild("quagmire_crate")
    inst.AnimState:PlayAnimation("idle",false)

	inst:AddTag("irreplaceable")        ---- 不给带出当前世界
	inst:AddTag("nonpotatable")         ---- 不会腐烂？！     

    MakeObstaclePhysics(inst, .75)

    inst.entity:SetPristine()

    add_container_before_not_ismastersim_retrun(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")
    inst:AddComponent("inspectable") --可检查组件   
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable()["box.name"])
    inst.components.inspectable:SetDescription(GetStringsTable()["box.inspect"])   --人物检查的描述
    -----------------------------------------------------------------------------------------------------------------------------------------------
    inst:ListenForEvent("onclose",function()
        inst.components.container:DropEverything()
        inst.AnimState:PlayAnimation("unwrap")
        inst:ListenForEvent("animover",inst.Remove)
    end)
    -----------------------------------------------------------------------------------------------------------------------------------------------
    ----- 防腐败处理
        inst:ListenForEvent("itemget",function(inst,_table)
            if _table and _table.item then
                local tempItem = _table.item
                if tempItem.components.perishable then
                    tempItem.components.perishable:StopPerishing()
                end
            end
        end)

        inst:ListenForEvent("itemlose",function(inst,_table)
            if _table and _table.prev_item then
                local tempItem = _table.prev_item
                if tempItem.components.perishable then
                    tempItem.components.perishable:StartPerishing()
                    tempItem.components.perishable:SetPercent(1)
                end
            end
        end)
    -----------------------------------------------------------------------------------------------------------------------------------------------
    return inst
end

return Prefab("npc_item_woodlegs_treasure_box", fn,assets)